document.getElementById("select").onchange=function(){
		var val=$(this).val()
		if(val==="添加习题"){
			return false;
		}
		delete window.publish_inventory
		$("#select").find("option").attr("selected",false)
		$("#select").find("option[value='添加习题']").attr("selected",true)
        	window.open ("publish_inventory.html?type="+val, "_blank", "height=800, width=1300, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
}

function delInventory(e){
	showErrorInfo("习题不可删除，因为别的地方有引用！")
	return false
	var id=e.getAttribute("sid")
	$.ajax({
		"url":"/inventory/",
		"type":"DELETE",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"id":id},
		"success":function(data){
			data=JSON.parse(data)
			if(!data.status){
				showErrorInfo(data.content)
				return false
			}
			showSuccessNotic()
			$(e).parent().parent().remove()
		}
	})
}
function getList(){
	$.ajax({
		"url":"/inventory/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			var tab=document.getElementById("tab")
			data=data.content;
			for(var i=0;i<data.length;i++){
				var tr=document.createElement("tr")
				var td=document.createElement("td")
				td.textContent=data[i].content;
				tr.appendChild(td)
			
				var td=document.createElement("td")
				td.textContent=data[i].teacher_username;
				tr.appendChild(td)
			
				var td=document.createElement("td")
				td.textContent=data[i].teacher_chinesename;
				tr.appendChild(td)
			
				var td=document.createElement("td")
				td.textContent=data[i].type;
				tr.appendChild(td)
			
				var td=document.createElement("td")
				td.textContent=data[i].value;
				tr.appendChild(td)
			
				var td=document.createElement("td")
				td.style.cssText="text-overflow: ellipsis ;max-width:400px;white-space: nowrap;overflow: hidden"
				td.textContent=data[i].name;
				tr.appendChild(td)
			
				var td=document.createElement("td")
				var t=""
				var answer=data[i].answer
				answer=JSON.parse(answer)
				for(var x=0;x<answer.length;x++){
					if(x+1===answer.length){
						t+=answer[x].toUpperCase()
					}
					else{
						t+=answer[x].toUpperCase()+ "、"
					}
				}
				td.textContent=t;
				tr.appendChild(td)
			
				var td=document.createElement("td")
				var btn=document.createElement("button")
				btn.className="btn btn-xs btn-primary"
				btn.setAttribute("data",JSON.stringify(data[i]))
				btn.textContent="编辑"
				btn.onclick=function(){
					var _data=this.getAttribute("data")
					window.publish_inventory=JSON.parse(_data)
					console.log(window.publish_inventory)
        				window.open ("publish_inventory.html?type="+window.publish_inventory.type, "_blank", "height=800, width=1300, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
				}
				td.appendChild(btn)
				var btn=document.createElement("button")
				btn.className="btn btn-xs btn-danger"
				btn.setAttribute("sid",data[i].id)
				btn.style.marginLeft="5px"
				btn.onclick=function(){
					if(confirm("真的要删除吗？")===false){
						return false;
					}
					var sid=this.getAttribute("sid")
					$.ajax({
						"url":"/inventory/",
						"type":"DELETE",
						"data":{"id":sid},
						"error":errorAjax,
						"beforeSend":start_load_pic,
						"complete":stop_load_pic,
						"success":function(data){
							data=JSON.parse(data)
							if(!data.status){
								showErrorInfo(data.content)
								return false;
							}
							alert("操作成功！")
							window.location.reload()
						}
					})
				}
				
				btn.textContent="删除"
				if (data[i].delete===true){
					//只有管理员和归属用户可以删除
					td.appendChild(btn)
				}

				var btn=document.createElement("button")
				btn.className="btn btn-sm btn-danger"
				btn.setAttribute("sid",data[i].id)
				btn.style.marginLeft="4px"
				btn.textContent="删除"
				btn.onclick=function(){
					delInventory(this)
				}
				//td.appendChild(btn)
				tr.appendChild(td)
				
				tab.appendChild(tr)
			
			}
		}
	})
}
getList()
